#include "stdafx.h"
#include "ProcessManagerInterface.h"
#include "ProcessManager.h"

using namespace ProcessManager;

//**********************************************************
//function:
bool CProcessManagerInterface::closeProcess(DWORD vProcessId)
{
    return CProcessManager::getInstance()->closeProcess(vProcessId);
}

//**********************************************************
//function:
DWORD CProcessManagerInterface::getOrCreateProcess(const wchar_t* vPath, const wchar_t* vCommandLine)
{
    _ASSERTE((vPath == NULL && vCommandLine != NULL) || (vPath != NULL));
    wchar_t CommandLine[MAX_PATH];
    if (vPath != NULL && vCommandLine == NULL)
    {
        swprintf(CommandLine, MAX_PATH, L"%s", vPath);
    }
    else
    {
        swprintf(CommandLine, MAX_PATH, L"%s", vCommandLine);
    }
    return CProcessManager::getInstance()->getOrCreateProcess(vPath, CommandLine);
}

//**********************************************************
//function:
DWORD CProcessManagerInterface::getOrCreateProcess(const std::string& vPath, const std::string& vCommandLine)
{
    wchar_t* Path = __convertString2wchar(vPath);
    wchar_t* CommandLine = __convertString2wchar(vCommandLine);
    return getOrCreateProcess(Path, CommandLine);
}

//**********************************************************
//function:
DWORD CProcessManagerInterface::getProcessIDByName(const std::string& vProcessName)
{
    wchar_t* ProcessName = __convertString2wchar(vProcessName);
    return CProcessManager::getInstance()->getProcessIDByName(ProcessName);
}

//**********************************************************
//function:
bool ProcessManager::CProcessManagerInterface::IsProcessRunning(const std::string& vProcessName)
{
    DWORD ProcessID = getProcessIDByName(vProcessName);
    return ProcessID != (DWORD)INVALID_HANDLE_VALUE;
}

//**********************************************************
//function:
wchar_t* ProcessManager::CProcessManagerInterface::__convertString2wchar(const std::string& vStr)
{
    const char* pCStrKey = vStr.c_str();
    int Size = MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, NULL, 0);
    wchar_t* pWCStrKey = new wchar_t[Size];
    MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, pWCStrKey, Size);
    return pWCStrKey;
}
